﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;

namespace FileSystemAudit
{
    public class FolderCollection : Collection<Folder>, IDisposable
    {
        private static FolderCollection _current;

        public static FolderCollection Current
        {
            get
            {
                if (_current == null)
                    _current = new FolderCollection(true);
                return _current;
            }
        }
        private string folderToSave = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\FileSystemAudit";

        public FolderCollection()
        {
        }

        public FolderCollection(bool prepare)
        {
            if (prepare) Load();
        }

        protected internal System.Data.DataSet dataSetConfiguration;

        public bool Load()
        {
            bool res = false;

            try
            {
                dataSetConfiguration = new DataSet();
                FileInfo fi = new FileInfo(folderToSave + "\\" + "data.xml");
                if (!fi.Exists)
                    return res;

                StreamReader sr = fi.OpenText();

                dataSetConfiguration.ReadXml(sr);
                sr.Close();
                DataTable dtFolders = dataSetConfiguration.Tables["Folders"];

                foreach (DataRow rw in dtFolders.Rows)
                {
                    Folder folder = new Folder();
                    try
                    {
                        folder.Unit = rw["Unit"].ToString();
                        folder.ActivateSniffer = (bool)rw["ActivateSniffer"];
                        folder.Order = (int)rw["order"];
                        folder.Path = rw["Path"].ToString();
                    }
                    catch (Exception)
                    {

                    }

                    this.Add(folder);
                }

                res = true;
            }
            catch (Exception exc)
            {
                res = false;
            }
            return res;
        }

        internal void Save()
        {
            FileInfo fi = new FileInfo(folderToSave + "\\" + "data.xml");
            //FileStream fs = fi.OpenWrite();
            StreamWriter sw = fi.CreateText();
        }


        #region IDisposable Members

        public void Dispose()
        {
            if (dataSetConfiguration != null)
                dataSetConfiguration.Dispose();
        }

        #endregion
    }

    public class Folder
    {
        public Folder() { }

        public string Unit;
        public string Path;
        public int Order;
        public bool ActivateSniffer;

        public FileSystemWatcher fsw;

    }
}
